package com.corejsf;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.hibernate.Session;

import com.corejsf.util.SessionFactoryUtil;

@ManagedBean(name="tableBean")
@SessionScoped
public class TableBean implements Serializable {

	private List<Person> persons;

	public TableBean() {
		persons = new ArrayList<Person>();
		//persons.add(new Person("Іван", "Іванович", "Іваненко", 30, "Львів"));
		/*persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));
		persons.add(new Person("Ivan", "Ivanovych", "Ivanenko", 30, "Lviv"));
		persons.add(new Person("Petro", "Petrovych", "Petrenko", 20, "Stryi"));*/
	}

	public List<Person> getPersons() {
		final Session session = SessionFactoryUtil.getSessionFactory().openSession();
		session.beginTransaction();
		final List<Person> list = session.createQuery("from Person").list();
		persons.clear();
		persons.addAll(list);
		session.getTransaction().commit();
		return persons;
	}

}
